# -*- coding: utf-8 -*-
# @Time    : 2021/4/2 10:26
# @Author  : zcn
# @Software: PyCharm
import difflib
import json

from fastapi import APIRouter, Request

from apps.allsubdb.err_analysis_server.models import ServerGetExcept
from apps.core.global_model import InputPlatformModel
from apps.core.m_route import ContextIncludedRoute
from apps.core.return_info import ReturnInfo, FAILED, SUCCESS
from apps.sql_app.database import proxys_sql_conn
from apps.sql_app.mmongodb import Coll

router = APIRouter(route_class=ContextIncludedRoute)
# 做个测试11111111111
async def ond_dict_solve():
    pass


async def sql_delete_as():
    sql = "delete * from err_analysis"
    from main import app
    pool = app.state.pool
    async with pool.acquire() as conn:
        async with conn.cursor() as cur:
            await cur.execute(sql)
            await conn.commit()
    return True


#
async def str_compare(str1, str2):
    # str1为对比参照字符串, str2为对比字符串, 若对比度大于0.9(待定),则认为相似,返回参照字符串继续与下一个对比,否则
    nums = difflib.SequenceMatcher(None, str1, str2).quick_ratio()
    if nums > 0.9:
        return True, str1
    else:
        return False, str2


# 目前的想法是先判定result_info 若存在 则分析里面msg_code作为标志,进字典   若不存在   分析traceback_str 以字符串对比形式入字典, 可设置字典默认值get(key,默认value)
# 如果key不存在,则放入一个为1 的, 否则对值加一
@router.get("/errs_analysis/err_solve")
async def get_server():
    data_dict = dict()
    msg_dict = dict()
    return_info = ReturnInfo()
    err_infos = Coll().get_errs()
    async for one_data in err_infos.find({}):
        traceback_str = one_data["traceback_str"]
        result_info = one_data["result_info"]
        if return_info:
            msgs = json.loads(return_info)
            msg_code = msgs["msg_code"]

        # await request.app.state.redis
    # cjip = err_infos.find({})
    # async for one_data in cjip:
    #     kp = await one_data["_id"]
    #     print(kp)
    return_info.status = SUCCESS
    return_info.msg_code = 200
    return_info.msg = "代理获取成功"
    return_info.data = ""
    return return_info.todict()

